<?php

class Reward_qualifiedController extends AdminController {

    public $db;

    public function actionView() {
        $sort = new CSort;
        $sort->attributes = array(
            'reward_node_right' => array(
                'asc' => 'reward_node_right',
                'desc' => 'reward_node_right DESC',
            ),
            'reward_node_left' => array(
                'asc' => 'reward_node_left',
                'desc' => 'reward_node_left DESC',
            ),
            'network_mid' => array(
                'asc' => 'network_mid',
                'desc' => 'network_mid DESC',
            ),
        );


        $data_arr = array(
            'select' => '*',
            'from' => 'sys_reward sr',
            'join' => array(
                'sn' => 'sys_network sn',
                'sm' => 'sys_member sm',
            ),
            'params_join' => array(
                'sn' => 'sr.reward_network_id=sn.network_id',
                'sm' => 'sn.network_id=sm.member_network_id',
            ),
            'order' => 'network_id',
            'where' => 'reward_is_qualified=:qualified',
            'params' => array(':qualified' => '1'),
        );

        $results = $this->get_data($data_arr);
        $dataProvider = new CArrayDataProvider($results,
                        array(
                            'keyField' => 'network_id',
                            'sort' => $sort,
                ));




        $this->render('view', array(
            'dataProvider' => $dataProvider,
        ));
    }
    
    public function actionViewWarehouse() {
        $sort = new CSort;
        $sort->attributes = array(
            'reward_node_right' => array(
                'asc' => 'reward_node_right',
                'desc' => 'reward_node_right DESC',
            ),
            'reward_node_left' => array(
                'asc' => 'reward_node_left',
                'desc' => 'reward_node_left DESC',
            ),
            'network_mid' => array(
                'asc' => 'network_mid',
                'desc' => 'network_mid DESC',
            ),
        );


        $data_arr = array(
            'select' => '*',
            'from' => 'sys_reward_warehouse sr',
            'join' => array(
                'sn' => 'sys_network sn',
                'sm' => 'sys_member sm',
            ),
            'params_join' => array(
                'sn' => 'sr.reward_network_id=sn.network_id',
                'sm' => 'sn.network_id=sm.member_network_id',
            ),
            'order' => 'network_id',
            'where' => 'reward_is_qualified=:qualified',
            'params' => array(':qualified' => '1'),
        );

        $results = $this->get_data($data_arr);
        $dataProvider = new CArrayDataProvider($results,
                        array(
                            'keyField' => 'network_id',
                            'sort' => $sort,
                ));




        $this->render('view_warehouse', array(
            'dataProvider' => $dataProvider,
        ));
    }
    
    public function actionViewQiradh() {
        $sort = new CSort;
        $sort->attributes = array(
            'reward_node_right' => array(
                'asc' => 'reward_node_right',
                'desc' => 'reward_node_right DESC',
            ),
            'reward_node_left' => array(
                'asc' => 'reward_node_left',
                'desc' => 'reward_node_left DESC',
            ),
            'network_mid' => array(
                'asc' => 'network_mid',
                'desc' => 'network_mid DESC',
            ),
        );


        $data_arr = array(
            'select' => '*',
            'from' => 'sys_reward_qord sr',
            'join' => array(
                'sn' => 'sys_network sn',
                'sm' => 'sys_member sm',
            ),
            'params_join' => array(
                'sn' => 'sr.reward_network_id=sn.network_id',
                'sm' => 'sn.network_id=sm.member_network_id',
            ),
            'order' => 'network_id',
            'where' => 'reward_is_qualified=:qualified',
            'params' => array(':qualified' => '1'),
        );

        $results = $this->get_data($data_arr);
        $dataProvider = new CArrayDataProvider($results,
                        array(
                            'keyField' => 'network_id',
                            'sort' => $sort,
                ));




        $this->render('view_qord', array(
            'dataProvider' => $dataProvider,
        ));
    }
    
    public function actionViewHaji() {
        $sort = new CSort;
        $sort->attributes = array(
            'reward_node_right' => array(
                'asc' => 'reward_node_right',
                'desc' => 'reward_node_right DESC',
            ),
            'reward_node_left' => array(
                'asc' => 'reward_node_left',
                'desc' => 'reward_node_left DESC',
            ),
            'network_mid' => array(
                'asc' => 'network_mid',
                'desc' => 'network_mid DESC',
            ),
        );


        $data_arr = array(
            'select' => '*',
            'from' => 'sys_reward_haji_plus sr',
            'join' => array(
                'sn' => 'sys_network sn',
                'sm' => 'sys_member sm',
            ),
            'params_join' => array(
                'sn' => 'sr.reward_network_id=sn.network_id',
                'sm' => 'sn.network_id=sm.member_network_id',
            ),
            'order' => 'network_id',
            'where' => 'reward_is_qualified=:qualified',
            'params' => array(':qualified' => '1'),
        );

        $results = $this->get_data($data_arr);
        $dataProvider = new CArrayDataProvider($results,
                        array(
                            'keyField' => 'network_id',
                            'sort' => $sort,
                ));




        $this->render('view_haji_plus', array(
            'dataProvider' => $dataProvider,
        ));
    }
    public function actionViewAmWarehouse() {
        $sort = new CSort;
        $sort->attributes = array(
            'reward_node_right' => array(
                'asc' => 'reward_node_right',
                'desc' => 'reward_node_right DESC',
            ),
            'reward_node_left' => array(
                'asc' => 'reward_node_left',
                'desc' => 'reward_node_left DESC',
            ),
            'network_mid' => array(
                'asc' => 'network_mid',
                'desc' => 'network_mid DESC',
            ),
        );


        $data_arr = array(
            'select' => '*',
            'from' => 'sys_reward_am_warehouse aw',
            'join' => array(
                'sn' => 'sys_network sn',
                'sm' => 'sys_member sm',
            ),
            'params_join' => array(
                'sn' => 'aw.reward_network_id=sn.network_id',
                'sm' => 'sn.network_id=sm.member_network_id',
            ),
            'order' => 'network_id',
            'where' => 'reward_is_qualified=:qualified',
            'params' => array(':qualified' => '1'),
        );

        $results = $this->get_data($data_arr);
        $dataProvider = new CArrayDataProvider($results,
                        array(
                            'keyField' => 'network_id',
                            'sort' => $sort,
                ));




        $this->render('view_am_warehouse', array(
            'dataProvider' => $dataProvider,
        ));
    }

    public function Actionlist() {
        $mid = function_lib::uri_segment(5);
        $network_id = function_lib::get_network_id($mid);
        $file_name = "data_reward_" . $mid;
        Yii::import('application.extensions.phpexcel.JPhpExcel');
        $xls = new JPhpExcel('UTF-8', false, 'Data Reward');
        $arr_reward[1] = array('Data Reward ' . $mid);

        array_push($arr_reward, array('Tanggal', 'ID Downline', 'Posisi', 'Level'));

        $data_reward = Yii::app()->db->createCommand()->select('*')
                ->from('sys_reward_netgrow')
                ->where('reward_netgrow_network_id=:network_id', array('network_id' => $network_id))
                ->order(array('reward_netgrow_position','reward_netgrow_date'))->queryAll();
        //echo "<pre>";
        //print_r($data_reward);
        //die();
        foreach ($data_reward as $var => $data) {
            array_push($arr_reward, array($data['reward_netgrow_date'], function_lib::get_network_mid($data['reward_netgrow_downline_network_id']), $data['reward_netgrow_position'], $data['reward_netgrow_level']));
        }

        $xls->addArray($arr_reward);
        $xls->generate_excel(dirname($_SERVER['SCRIPT_FILENAME']) . "/excel/withdraw/" . $file_name);
        //Yii::app()->user->setFlash('success', 'Please <a href="' . Yii::app()->baseUrl . '/excel/withdraw/' . $file_name . '.xls">download file excel</a>.');
        $this->redirect(Yii::app()->baseUrl . '/excel/withdraw/' . $file_name . '.xls'); 
        exit();
    }
    
    public function Actionlistwarehouse() {
        $mid = function_lib::uri_segment(5);
        $network_id = function_lib::get_network_id($mid);
        $file_name = "data_reward_warehouse_" . $mid;
        Yii::import('application.extensions.phpexcel.JPhpExcel');
        $xls = new JPhpExcel('UTF-8', false, 'Data Reward');
        $arr_reward[1] = array('Data Reward ' . $mid);

        array_push($arr_reward, array('Tanggal', 'ID Downline', 'Posisi', 'Level'));

        $data_reward = Yii::app()->db->createCommand()->select('*')
                ->from('sys_reward_warehouse_netgrow')
                ->where('reward_netgrow_network_id=:network_id', array('network_id' => $network_id))
                ->order(array('reward_netgrow_position','reward_netgrow_date'))->queryAll();
        //echo "<pre>";
        //print_r($data_reward);
        //die();
        foreach ($data_reward as $var => $data) {
            array_push($arr_reward, array($data['reward_netgrow_date'], function_lib::get_network_mid($data['reward_netgrow_downline_network_id']), $data['reward_netgrow_position'], $data['reward_netgrow_level']));
        }

        $xls->addArray($arr_reward);
        $xls->generate_excel(dirname($_SERVER['SCRIPT_FILENAME']) . "/excel/withdraw/" . $file_name);
        //Yii::app()->user->setFlash('success', 'Please <a href="' . Yii::app()->baseUrl . '/excel/withdraw/' . $file_name . '.xls">download file excel</a>.');
        $this->redirect(Yii::app()->baseUrl . '/excel/withdraw/' . $file_name . '.xls'); 
        exit();
    }
    
    public function Actionlistqiradh() {
        $mid = function_lib::uri_segment(5);
        $network_id = function_lib::get_network_id($mid);
        $file_name = "data_reward_qiradh_" . $mid;
        Yii::import('application.extensions.phpexcel.JPhpExcel');
        $xls = new JPhpExcel('UTF-8', false, 'Data Reward');
        $arr_reward[1] = array('Data Reward ' . $mid);

        array_push($arr_reward, array('Tanggal', 'ID Downline', 'Posisi', 'Level'));

        $data_reward = Yii::app()->db->createCommand()->select('*')
                ->from('sys_reward_qord_netgrow')
                ->where('reward_netgrow_network_id=:network_id', array('network_id' => $network_id))
                ->order(array('reward_netgrow_position','reward_netgrow_date'))->queryAll();
        //echo "<pre>";
        //print_r($data_reward);
        //die();
        foreach ($data_reward as $var => $data) {
            array_push($arr_reward, array($data['reward_netgrow_date'], function_lib::get_network_mid($data['reward_netgrow_downline_network_id']), $data['reward_netgrow_position'], $data['reward_netgrow_level']));
        }

        $xls->addArray($arr_reward);
        $xls->generate_excel(dirname($_SERVER['SCRIPT_FILENAME']) . "/excel/withdraw/" . $file_name);
        //Yii::app()->user->setFlash('success', 'Please <a href="' . Yii::app()->baseUrl . '/excel/withdraw/' . $file_name . '.xls">download file excel</a>.');
        $this->redirect(Yii::app()->baseUrl . '/excel/withdraw/' . $file_name . '.xls'); 
        exit();
    }
    
    public function Actionlisthaji() {
        $mid = function_lib::uri_segment(5);
        $network_id = function_lib::get_network_id($mid);
        $file_name = "data_reward_haji_plus_" . $mid;
        Yii::import('application.extensions.phpexcel.JPhpExcel');
        $xls = new JPhpExcel('UTF-8', false, 'Data Reward');
        $arr_reward[1] = array('Data Reward ' . $mid);

        array_push($arr_reward, array('Tanggal', 'ID Downline', 'Posisi', 'Level'));

        $data_reward = Yii::app()->db->createCommand()->select('*')
                ->from('sys_reward_haji_plus_netgrow')
                ->where('reward_netgrow_network_id=:network_id', array('network_id' => $network_id))
                ->order(array('reward_netgrow_position','reward_netgrow_date'))->queryAll();
        //echo "<pre>";
        //print_r($data_reward);
        //die();
        foreach ($data_reward as $var => $data) {
            array_push($arr_reward, array($data['reward_netgrow_date'], function_lib::get_network_mid($data['reward_netgrow_downline_network_id']), $data['reward_netgrow_position'], $data['reward_netgrow_level']));
        }

        $xls->addArray($arr_reward);
        $xls->generate_excel(dirname($_SERVER['SCRIPT_FILENAME']) . "/excel/withdraw/" . $file_name);
        //Yii::app()->user->setFlash('success', 'Please <a href="' . Yii::app()->baseUrl . '/excel/withdraw/' . $file_name . '.xls">download file excel</a>.');
        $this->redirect(Yii::app()->baseUrl . '/excel/withdraw/' . $file_name . '.xls'); 
        exit();
    }

    public function Actionlistamwarehouse() {
        $mid = function_lib::uri_segment(5);
        $network_id = function_lib::get_network_id($mid);
        $file_name = "data_reward_am_warehouse_" . $mid;
        Yii::import('application.extensions.phpexcel.JPhpExcel');
        $xls = new JPhpExcel('UTF-8', false, 'Data Reward');
        $arr_reward[1] = array('Data Reward ' . $mid);

        array_push($arr_reward, array('Tanggal', 'ID Downline', 'Posisi', 'Level'));

        $data_reward = Yii::app()->db->createCommand()->select('*')
                ->from('sys_reward_am_warehouse_netgrow')
                ->where('reward_netgrow_network_id=:network_id', array('network_id' => $network_id))
                ->order(array('reward_netgrow_position','reward_netgrow_date'))->queryAll();
        //echo "<pre>";
        //print_r($data_reward);
        //die();
        foreach ($data_reward as $var => $data) {
            array_push($arr_reward, array($data['reward_netgrow_date'], function_lib::get_network_mid($data['reward_netgrow_downline_network_id']), $data['reward_netgrow_position'], $data['reward_netgrow_level']));
        }

        $xls->addArray($arr_reward);
        $xls->generate_excel(dirname($_SERVER['SCRIPT_FILENAME']) . "/excel/withdraw/" . $file_name);
        //Yii::app()->user->setFlash('success', 'Please <a href="' . Yii::app()->baseUrl . '/excel/withdraw/' . $file_name . '.xls">download file excel</a>.');
        $this->redirect(Yii::app()->baseUrl . '/excel/withdraw/' . $file_name . '.xls'); 
        exit();
    }
    
    private function get_data($data_arr) {
        if (!isset($this->db)) {
            $this->db = Yii::app()->db->createCommand();
        }
        $this->db->select($data_arr['select']);
        $this->db->from($data_arr['from']);
        if ($data_arr['where'] != '') {
            $this->db->where($data_arr['where'], $data_arr['params']);
        }
        //  array('not in','network_level_status_log_value','passive'),
        // array('>=','date(network_level_status_log_datetime)',$start_daily)
        if (isset($data_arr['join'])) {
            foreach ($data_arr['join'] AS $key => $val) {

                $this->db->join($val, $data_arr['params_join'][$key]);
            }
        }

        if (isset($data_arr['group']) AND trim($data_arr['group']) != '') {
            $this->db->group($data_arr['group']);
        }
        if (isset($data_arr['order']) AND trim($data_arr['order']) != '') {
            $this->db->order($data_arr['order']);
        }

        return $this->db->queryAll();
    }

}

?>
